package com.bstek.bdf4.utils.file;

import cn.hutool.core.util.StrUtil;
import com.bstek.bdf4.enums.ZipLevelEnum;
import lombok.experimental.UtilityClass;
import lombok.extern.slf4j.Slf4j;
import net.lingala.zip4j.core.ZipFile;
import net.lingala.zip4j.exception.ZipException;
import net.lingala.zip4j.model.ZipParameters;
import net.lingala.zip4j.util.Zip4jConstants;

import java.io.File;

/**
 *   http://www.lingala.net/zip4j/
 *   压缩解压工具类
 */
@UtilityClass
@Slf4j
public class BaseZipUtils {
    /**
     * 进行zip压缩
     * @param zipFileFullName 压缩包全路径
     * @param archiverFile 待加入压缩包的文件或目录
     * @param password 密码（若不传则表示非加密压缩）
     * @param level 压缩级别
     * @return
     */
    public boolean zip(String zipFileFullName, File archiverFile, String password, ZipLevelEnum level) {
        if (StrUtil.isEmpty(zipFileFullName)) {
            throw new IllegalArgumentException("zipFileFullName is null");
        }

        if (archiverFile == null) {
            throw new IllegalArgumentException("archiverFile is null");
        }
        if (level == null) {
            throw new IllegalArgumentException("level is null");
        }
        try {
            //生成的压缩文件
            ZipFile zipFile = new ZipFile(zipFileFullName);
            ZipParameters parameters = new ZipParameters();
            //压缩级别
            parameters.setCompressionMethod(Zip4jConstants.COMP_DEFLATE);
            parameters.setCompressionLevel(level.code);

            //加密压缩
            if (StrUtil.isNotEmpty(password)) {
                parameters.setEncryptFiles(true);
                parameters.setEncryptionMethod(Zip4jConstants.ENC_METHOD_AES);
                parameters.setAesKeyStrength(Zip4jConstants.AES_STRENGTH_256);
                parameters.setPassword(password);
            }

            //添加打包内容
            if (archiverFile.isDirectory()) {
                File[] fs = archiverFile.listFiles();
                for (File f : fs) {
                    if (f.isDirectory()) {
                        zipFile.addFolder(f.getPath(), parameters);
                    } else {
                        zipFile.addFile(f, parameters);
                    }
                }
            } else {
                zipFile.addFile(archiverFile, parameters);
            }
            return true;
        } catch (ZipException e) {
            log.error("压缩文件 {} 到 {}失败",archiverFile.getName(),zipFileFullName,e);
        }

        return false;
    }

    /**
     * zip解压缩
     * @param zipFileFullName 压缩文件全路径
     * @param destFullName 解压缩目标全路径
     * @param password 密码
     * @return
     */
    public boolean unzip(String zipFileFullName,String destFullName,String password) {
        if (StrUtil.isEmpty(zipFileFullName)) {
            throw new IllegalArgumentException("zipFileFullName is null");
        }

        if (StrUtil.isEmpty(destFullName)) {
            throw new IllegalArgumentException("destFullName is null");
        }

        try {
            ZipFile zipFile = new ZipFile(zipFileFullName);
            if (StrUtil.isNotEmpty(password) && zipFile.isEncrypted()) {
                zipFile.setPassword(password);
            }
            zipFile.extractAll(destFullName);
            return true;
        } catch (ZipException e) {
            log.error("解压文件 {} 到 {}失败",zipFileFullName,destFullName,e);
        }
        return false;
    }
}
